package com.sgai.zero_robot.ui.splash;


import android.Manifest;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.provider.Settings;
import androidx.annotation.NonNull;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;

import com.sgai.zero_robot.base.BaseViewModel;
import com.sgai.zero_robot.base.BaseActivity;
import com.sgai.zero_robot.database.ZeroDetectionRepository;
import com.sgai.zero_robot.databinding.ActivitySplashBinding;
import com.sgai.zero_robot.ui.alarm.AlarmUtils;
import com.sgai.zero_robot.ui.login.LoginActivity;
import com.sgai.zero_robot.utils.LogUtil;

import java.util.ArrayList;
import java.util.List;

public class SplashActivity extends BaseActivity<ActivitySplashBinding, BaseViewModel> {
    private final String[] permissions = {Manifest.permission.WRITE_EXTERNAL_STORAGE,Manifest.permission.ACCESS_NETWORK_STATE,
    };

    private boolean isStartManage = false;

    @Override
    public void initViews(Bundle savedInstanceState) {
        LogUtil.d(TAG, "initViews: ");
        if (checkManage(true)) {
            LogUtil.d(TAG, "initViews: 1");
            if (checkPermissions(true)) {
                LogUtil.d(TAG, "initViews: 2");
                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        initSDK();
                    }
                },3000);
            }
        }
    }

    @Override
    public void loadData() {

    }

    private boolean checkManage(boolean isRequest) {
        if (Settings.System.canWrite(SplashActivity.this)) {
            LogUtil.d(TAG, "checkManage: true");
            // 已经有权限
            return true;
        } else {
            if (isRequest) {
                Intent aPackage = new Intent(Settings.ACTION_MANAGE_WRITE_SETTINGS,
                        Uri.fromParts("package", getPackageName(), null));
                aPackage.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                startActivity(aPackage);
                isStartManage = true;
            }
            LogUtil.d(TAG, "checkManage: false");
            return false;
        }
    }

    private boolean tag = false;

    @Override
    protected void onPause() {
        super.onPause();
        LogUtil.d(TAG, "onPause: " + isStartManage);
        if (isStartManage) {
            isStartManage = false;
            tag = true;
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        LogUtil.d(TAG, "onResume: " + tag);
        if (tag) {
            tag = false;
            if (checkManage(false)) {
                checkPermissions(true);
            } else {
                showToast("请手动开启下权限");
            }
        }
    }

    private void checkPermission(List<String> temp, String permission) {
        if (ContextCompat.checkSelfPermission(getApplicationContext(), permission) != PackageManager.PERMISSION_GRANTED) {
            temp.add(permission);
        }
    }

    private boolean checkPermissions(boolean isRequest) {
        // 已经有权限
        // 检查该权限是否已经获取
        List<String> temp = new ArrayList<>();
        checkPermission(temp, permissions[0]);

        // 权限是否已经 授权 GRANTED---授权  DENIED---拒绝
        if (temp.size() > 0) {
            if (isRequest) {
                String[] s = new String[temp.size()];
                for (int i = 0; i < temp.size(); i++) {
                    s[i] = temp.get(i);
                }
                // 如果没有授予该权限，就去提示用户请求
                ActivityCompat.requestPermissions(SplashActivity.this, s, 100);
            }
            return false;
        } else {
            return true;
        }
    }

    private void initSDK() {
        LogUtil.init();
        AlarmUtils.init();
        ZeroDetectionRepository.init(this);
        startActivity(new Intent(this, LoginActivity.class));
        finish();
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        LogUtil.d(TAG, "onRequestPermissionsResult: requestCode = " + requestCode);
        if (checkPermissions(false)) {
            initSDK();
        } else {
            showToast("请手动开启权限");
            finish();
        }
    }

}